<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入组织名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="姓名" prop="location">
            <el-input
              v-model="queryParams.tname"
              placeholder="请输入姓名"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="毕业学校" prop="graduationSchool">
            <el-input
              v-model="queryParams.graduationSchool"
              placeholder="请输入毕业学校"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
              <el-option
                v-for="dict in sexOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['manage:teacherInfo:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['manage:teacherInfo:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['manage:teacherInfo:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['manage:teacherInfo:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="teacherInfoList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="教师信息ID" align="center" prop="id" v-if="false"/>
          <el-table-column label="账号" align="center" prop="userName" />
          <el-table-column label="姓名" align="center" prop="tname" />
          <el-table-column label="邮箱" align="center" prop="email" />
          <el-table-column label="电话" align="center" prop="phonenumber" />
          <el-table-column label="性别" align="center" prop="sex" >
          <template slot-scope="scope">
            <span v-if="scope.row.sex == 0">男</span>
            <span v-if="scope.row.sex == 1">女</span>
            <span v-if="scope.row.sex == 2">未知</span>
          </template>
          </el-table-column>
          <el-table-column label="地址" align="center" prop="location" />
          <el-table-column label="籍贯" align="center" prop="nativePlace" />
          <el-table-column label="政治面貌" align="center" prop="political" />
          <el-table-column label="生日" align="center" prop="birthday" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="教学时间" align="center" prop="teacherTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.teacherTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="毕业学校" align="center" prop="graduationSchool" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="tDetails(scope.row)"
              >详情</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['manage:teacherInfo:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['manage:teacherInfo:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"/>
      </el-col>
    </el-row>
    <!-- 添加或修改教师信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="640px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="关联账号" prop="account">
              <el-select v-model="form.account" @change="accountChange" filterable placeholder="请选择登录账号">
                <el-option
                  v-for="item in teacherOptions"
                  :key="item.userId"
                  :label="item.userName"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属组织" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属组织"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="tname">
              <el-input v-model="form.tname" placeholder="请输入教师姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择">
                <el-option
                  v-for="dict in sexOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="电话" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱"/>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="生日" prop="birthday">
              <el-date-picker clearable size="small"
                              v-model="form.birthday"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择生日">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="籍贯" prop="nativePlace">
              <el-input v-model="form.nativePlace" placeholder="请输入籍贯"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="政治面貌" prop="political">
              <el-input v-model="form.political" placeholder="请输入政治面貌"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="教学时间" prop="teacherTime">
              <el-date-picker clearable size="small" v-model="form.teacherTime" type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择教学时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="毕业学校" prop="graduationSchool">
              <el-input v-model="form.graduationSchool" placeholder="请输入毕业学校"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址" prop="location">
              <el-input v-model="form.location" placeholder="请输入详细地址"/>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    addTeacherInfo,
    delTeacherInfo,
    exportTeacherInfo,
    getTeacherInfo,
    listTInfoVo,
    updateTeacherInfo
  } from "@/api/manage/teacherinfo";
  import {treeselect} from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import {getListByRole} from "@/api/system/user";

  export default {
    name: "TeacherInfo",
    components: {
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 教师信息表格数据
        teacherInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          tname: undefined,
          account: undefined,
          location: undefined,
          nativePlace: undefined,
          political: undefined,
          teacherTime: undefined,
          graduationSchool: undefined,
          birthday: undefined,
          deptId: undefined,
          email: undefined,
          phonenumber: undefined,
          sex: undefined,
        avatar: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tname: [
          {required: true, message: "教师姓名不能为空", trigger: "blur"}
        ],
        account: [
          {required: true, message: "教师账号不能为空", trigger: "blur"}
        ],
        location: [
          {required: true, message: "详细地址不能为空", trigger: "blur"}
        ],
        nativePlace: [
          {required: true, message: "籍贯不能为空", trigger: "blur"}
        ],
        political: [
          {required: true, message: "政治面貌不能为空", trigger: "blur"}
        ],
        graduationSchool: [
          {required: true, message: "毕业学校不能为空", trigger: "blur"}
        ],
      },
        // 组织名称
        deptName: undefined,
        // 组织树选项
        deptOptions: undefined,
        defaultProps: {
          children: "children",
          label: "label"
        },
        teacherOptions: [],
        // 性别状态字典
        sexOptions: [],
      };
  },
  watch: {
    // 根据名称筛选组织树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
  },
  methods: {
    /** 查询组织下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
    /** 查询教师信息列表 */
    getList() {
      this.loading = true;
      listTInfoVo(this.queryParams).then(response => {
        this.teacherInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 关联账号change事件  */
    accountChange(data){
      for (const datum of this.teacherOptions) {
        if(datum.userId == data){
          this.form.deptId = datum.deptId
          this.form.tname = datum.nickName
          this.form.email = datum.email
          this.form.phonenumber = datum.phonenumber
          this.form.sex = datum.sex
          break
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        tname: undefined,
        account: undefined,
        location: undefined,
        nativePlace: undefined,
        political: undefined,
        teacherTime: undefined,
        graduationSchool: undefined,
        birthday: undefined,
        deptId: undefined,
        email: undefined,
        phonenumber: undefined,
        sex: undefined,
        avatar: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教师信息";
      let roleId = 101
      getListByRole(roleId).then(res => {
        this.teacherOptions = res.data
      })
    },

    /** 老师详情按钮操作 */
    tDetails(row){
      this.$router.push({ path: '/teacherinfo/tdetail', query: { tId: row.id } })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTeacherInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教师信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeacherInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeacherInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除教师信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTeacherInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教师信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTeacherInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
